package com.yvan.codeslave.batch;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.yvan.platform.Conv;
import com.yvan.platform.FileUtils;
import com.yvan.platform.JsonWapper;
import com.yvan.platform.YvanUtil;
import lombok.val;
import lombok.var;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class util {
    private static Pattern linePattern = Pattern.compile("_(\\w)");
    private static Pattern humpPattern = Pattern.compile("[A-Z]");

    /**
     * 根据 basePath 和 namespace 获取真实的文件路径
     */
    public static String basePathNamespacePath(String basePath, String namespace) {
        val namespaceList = Splitter.on(".").omitEmptyStrings().splitToList(namespace);
        val namespacePath =
                Joiner.on("/").join(
                        namespaceList.subList(0, namespaceList.size() - 1)
                );
        val namespaceFile = namespaceList.get(namespaceList.size() - 1) + ".java";
        return FileUtils.normalizePath(basePath, namespacePath, namespaceFile);
    }

    public static List<String> split(String strObj, String splitChar) {
        return Splitter.on(splitChar).splitToList(strObj);
    }

    public static Map<String, Object> asDic(Map innerMap, String path) {
        return new JsonWapper(innerMap).asMap(path.split("\\."));
    }

    public static Object asString(Map innerMap, String path) {
        return new JsonWapper(innerMap).get(path.split("\\."));
    }

    public static Set<String> distinct(Iterable<String> iterable) {
        return Sets.newLinkedHashSet(iterable);
    }

    public static String toYaml(Object obj){
        return YvanUtil.toYaml(obj);
    }

    /**
     * 去掉两边空格
     */
    public static String trim(Object a) {
        return Conv.NS(a).trim();
    }

    /**
     * 第一个字符转小写
     */
    public static String firstLower(Object a) {
        val s = Conv.NS(a);
        if (Strings.isNullOrEmpty(s)) {
            return s;
        }
        if (s.length() == 1) {
            return s.toLowerCase();
        }
        return s.substring(0, 1).toLowerCase() + s.substring(1);
    }

    /**
     * 转为小写
     */
    public static String low(Object a) {
        return Conv.NS(a).toLowerCase();
    }


    /**
     * 驼峰转下划线,效率比上面高
     */
    public static String line(Object a) {
        val str = firstLower(a);
        if (Strings.isNullOrEmpty(str)) {
            return str;
        }
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 下划线转驼峰
     */
    public static String hump(Object a) {
        var str = Conv.NS(a);
        if (Strings.isNullOrEmpty(str)) {
            return str;
        }
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 从namespace转className
     */
    public static String className(Object namespaceObj) {
        val namespaceList = Splitter.on(".").omitEmptyStrings().splitToList(Conv.NS(namespaceObj));
        return namespaceList.get(namespaceList.size() - 1);
    }

    /**
     * 从 namespace 转 namespacePath
     */
    public static String namespacePath(Object namespaceObj) {
        val namespaceList = Splitter.on(".").omitEmptyStrings().splitToList(Conv.NS(namespaceObj));
        return
                Joiner.on(".").join(
                        namespaceList.subList(0, namespaceList.size() - 1)
                );
    }

    /**
     * 缩进
     * @param tab
     * @return String
     */
    public static String tableIndent(int tab){
        String tabStr = "";
        for (int i = 0; i < tab; i++) {
            tabStr += "    ";
        }
        return tabStr;
    }

    /**
     * 生成字符串等长的空白字符串
     * @param str
     * @return String
     */
    public static String stringToSpaceString(String str){
        String spaceString = "";
        for (int i = 0; i < str.length(); i++) {
            spaceString += " ";
        }
        return spaceString;
    }

    public static boolean startsWith(String text, String prefix) {
        if(Strings.isNullOrEmpty(text)) {
            return false;
        }
        return text.startsWith(prefix);
    }

    public static List<String> tableNames(JsonWapper jw) {
        List<String> ret = Lists.newArrayList();
        if (jw.contains("Table", Consts.TABLE_ID)) {
            ret.add("Table");
        }
        for (int i = 2; jw.contains("Table" + i, Consts.TABLE_ID); i++) {
            ret.add("Table" + i);
        }
        return ret;
    }


    public static void main(String[] args) {
        Lists.newArrayList(
                hump(null),
                hump(""),
                hump("AAA"),
                hump("aaa"),
                hump("sys_dict"),
                hump("SysDict")
        ).stream().forEach(System.out::println);
    }
}
